package yf.mall.controller.api.app;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.annotation.SysLog;
import com.yf.response.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.service.NoticeService;
import yf.mall.service.app.LoginService;
import yf.mall.vo.app.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/api/app/mini/")
public class ApiAppLoginController {

    @Resource
    private NoticeService noticeService;

    @Resource
    private LoginService loginService;

    @RequestMapping("/captcha")
    @IgnoreAuth
    public R captcha(String mobile) {
        String captcha = noticeService.sendCaptcha(mobile);
        return R.ok(captcha);
    }


    @SysLog("手机号验证码登录")
    @PostMapping("/phone")
    @IgnoreAuth
    private R phoneLogin(@RequestBody PhoneLoginDTO phoneLoginDTO) {
        PhoneLoginVO phoneLoginVO = loginService.phoneLogin(phoneLoginDTO);
        return R.ok(phoneLoginVO);
    }

    @PostMapping("/code")
    @IgnoreAuth
    private R codeLogin(@RequestBody CodeLoginDTO codeLoginDTO) {
        CodeLoginVO codeLoginVO = loginService.codeLogin(codeLoginDTO);
        return R.ok(codeLoginVO);
    }

    @PostMapping("/wx/mini")
    @IgnoreAuth
    private R wxLogin(@RequestBody WxMiniLoginDTO wxLoginDTO) {
        WxMiniLoginVO wxMiniLoginVO = loginService.wxMiniLogin(wxLoginDTO);
        return R.ok(wxMiniLoginVO);
    }

    @PostMapping("/user/logout")
    private R logout(@LoginUserId Long userId) {
        boolean result = loginService.logout(userId);
        return R.ok(result);
    }


}
